package com.jinlia.show.modules.base.system.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.common.core.utils.PageUtils;
import com.jinlia.show.modules.base.system.entity.Goods;
import com.jinlia.show.modules.base.system.entity.GoodsType;
import com.jinlia.show.modules.base.system.mapper.GoodsTypeMapper;
import com.jinlia.show.modules.base.model.query.GoodsTypeQuery;
import com.jinlia.show.modules.base.model.po.GoodsPO;
import com.jinlia.show.modules.base.model.po.GoodsTypePO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class GoodsTypeMapperService extends ServiceImpl<GoodsTypeMapper, GoodsType> {

    final GoodsMapperService goodsMapperService;

    public Page<GoodsPO> goodsList(GoodsTypeQuery goodsTypeQuery) {
        Page<Goods> page = new Page<>(goodsTypeQuery.getCurrent(), goodsTypeQuery.getSize());
        //构造查询参数
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Objects.nonNull(goodsTypeQuery.getGoodsTypeId()), Goods::getGoodsType, goodsTypeQuery.getGoodsTypeId());
        lambdaQueryWrapper.eq(Objects.nonNull(goodsTypeQuery.getEnableStatus()), Goods::getEnableStatus, goodsTypeQuery.getEnableStatus());
        Page<Goods> page1 = goodsMapperService.page(page, lambdaQueryWrapper);
        Page<GoodsPO> sysUserVOPage = PageUtils.tToVO(page1, GoodsPO.class);
        return sysUserVOPage;
    }

    public List<GoodsTypePO> goodsListPO(GoodsTypeQuery goodsTypeQuery) {
        List<GoodsTypePO> collectPO = this.list().stream().map(GoodsTypePO::fromEntity).collect(Collectors.toList());
        //填充listGoods
        if (!CollectionUtils.isEmpty(collectPO)) {
            Map<Long, List<Goods>> collect1 = goodsMapperService
                    .lambdaQuery()
                    .eq(Objects.nonNull(goodsTypeQuery.getEnableStatus()),Goods::getEnableStatus,goodsTypeQuery.getEnableStatus())//支持状态查询
                    .eq(Objects.nonNull(goodsTypeQuery.getGoodsTypeId()), Goods::getGoodsType, goodsTypeQuery.getGoodsTypeId())
                    .list()
                    .stream().collect(Collectors.groupingBy(Goods::getGoodsType));
            collectPO.forEach(i -> i.setListGoods(collect1.get(i.getId())));
        }
        return collectPO;
    }

    public IPage<GoodsTypePO> getPage(GoodsTypeQuery goodsTypeQuery) {
        // 构建分页参数
        Page<GoodsTypePO> page = new Page<>(goodsTypeQuery.getCurrent(), goodsTypeQuery.getSize());
        //构造查询参数
        Page<GoodsTypePO> sysUserVOPage = this.getBaseMapper().getPagePO(page, goodsTypeQuery);
        return sysUserVOPage;
    }
}
